package serviceswarm

import (
	"context"
	"encoding/json"
	"github.com/aberic/gnomon"
	"github.com/docker/docker/api/types"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
	"gykjgit.dccnet.com.cn/chain/proto/swarm"
)

// ContainerLogs ContainerLogs
func ContainerLogs(url string, in *swarm.ReqContainerLogs) (*swarm.RespContainerLogs, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewContainerServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Logs(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespContainerLogs); ok {
			return response, nil
		}
		return nil, errors.New("container logs failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "container logs failed")
}

// ContainerList ContainerList
func ContainerList(url string) ([]types.Container, error) {
	var cs []types.Container
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewContainerServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.List(context.Background(), &swarm.ReqContainerList{})
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespContainerList); ok {
			if err = json.Unmarshal(response.Containers, &cs); nil == err {
				return cs, nil
			}
			return nil, errors.Wrap(err, "container list failed")
		}
		return nil, errors.New("container list failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "container list failed")
}

// ContainerInspect ContainerInspect
func ContainerInspect(url, containerID string) (*types.ContainerJSON, error) {
	var cj types.ContainerJSON
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewContainerServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Inspect(context.Background(), &swarm.ReqContainerInspect{ContainerID: containerID})
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespContainerInspect); ok {
			if err = json.Unmarshal(response.Container, &cj); nil == err {
				return &cj, nil
			}
			return nil, errors.Wrap(err, "container inspect failed")
		}
		return nil, errors.New("container inspect failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "container inspect failed")
}

// ContainerStats ContainerStats
func ContainerStats(url, containerID string) (*types.Stats, error) {
	var stats types.Stats
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewContainerServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Stats(context.Background(), &swarm.ReqContainerStats{ContainerID: containerID})
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespContainerStats); ok {
			if err = json.Unmarshal(response.Stats, &stats); nil == err {
				return &stats, nil
			}
			return nil, errors.Wrap(err, "container stats failed")
		}
		return nil, errors.New("container stats failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "container stats failed")
}

// ContainerStatsAll ContainerStatsAll
func ContainerStatsAll(url string) ([]*types.Stats, error) {
	var stats []*types.Stats
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := swarm.NewContainerServerClient(conn)
		//客户端向grpc服务端发起请求
		return cli.StatsAll(context.Background(), &swarm.ReqContainerStatsAll{})
	})
	if nil == err {
		if response, ok := resp.(*swarm.RespContainerStatsAll); ok {
			if err = json.Unmarshal(response.Stats, &stats); nil == err {
				return stats, nil
			}
			return nil, errors.Wrap(err, "container stats all failed")
		}
		return nil, errors.New("container stats all failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "container stats all failed")
}
